package qj.ui;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import javax.swing.SwingUtilities;

import qj.util.funct.P0e;
import qj.util.io.LimitedByteArrayOutputStream;

public class Console4 {
	public static void println(String s) {
		IConsole.SYSTEM.println(s);
	}
	
	public static void writeln(String s) {
		IConsole.SYSTEM.println(s);
	}
	
	public static String readLine() {
		return IConsole.SYSTEM.readLine();
	}

	public static String readLineUntil(String limit) {
		return IConsole.SYSTEM.readLineUntil(limit);
	}

	public static String readLineUntilDot() {
		return IConsole.SYSTEM.readLineUntilDot();
	}

	public static String readLineUntilDot(String prompt1) {
		return IConsole.SYSTEM.readLineUntilDot(prompt1);
	}

	public static String readLineUntilDot(String prompt1, String prompt2) {
		return IConsole.SYSTEM.readLineUntilDot(prompt1, prompt2);
	}

	public static String readLine(String str) {
		return IConsole.SYSTEM.readLine(str);
	}

    static PrintStream err;
	public static void hideSystemErr() {
        err = System.err;
        System.setErr(new PrintStream(new ByteArrayOutputStream())); // Redirect the useless warning messages
//		err.println("System err redirected");
	}
	public static void showSystemErr() {
        System.setErr(err);
	}

	public static void hideSystemErr(P0e run) {
        PrintStream err = System.err;
        System.setErr(new PrintStream(new ByteArrayOutputStream())); // Redirect the useless warning messages
        try {
            run.e();
        } catch (Exception e) {
            e.printStackTrace(err);
        }
        System.setErr(err);
	}
	
	private static LimitedByteArrayOutputStream wrappedConsole = new LimitedByteArrayOutputStream();
	public static void wrapStandardConsole() {
		System.out.println("Standard Output redirected");
		System.setErr(new PrintStream(wrappedConsole));
		System.setOut(new PrintStream(wrappedConsole));
	}
	
	public static void showWrappedConsole() {
		SwingUtilities.invokeLater(new Runnable() {public void run() {
			DesktopUI4.alert2(wrappedConsole.toString());
		}});
	}
	
}
